package com.xlhj.boot.jpa.service.impl;

import com.xlhj.boot.jpa.dao.EmpRepository;
import com.xlhj.boot.jpa.entity.EmpInfo;
import com.xlhj.boot.jpa.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

/**
 * @Author: caijliu
 * @Date: 10/11/2021 15:00
 * @Description TODO
 * @Vsersion: 0.0.1
 */
@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpRepository empRepository;

    @Override
    public EmpInfo insertEmpInfo(EmpInfo empInfo) {
        return empRepository.save(empInfo);
    }

    @Override
    public EmpInfo updateEmpInfo(EmpInfo empInfo) {
        return empRepository.save(empInfo);
    }

    @Override
    public EmpInfo findById(Long id) {
        return empRepository.findById(id).get();
    }

    @Override
    public Page<EmpInfo> findByPage(int pageNum, int pageSize) {
        Pageable pageable = PageRequest.of(pageNum, pageSize);
        return empRepository.findAll(pageable);
    }

    @Override
    public Page<EmpInfo> findByExampleAndPage(String empName, int pageNum, int pageSize) {
        EmpInfo empInfo = new EmpInfo();
        empInfo.setEmpName(empName);
        Pageable pageable = PageRequest.of(pageNum, pageSize);
        ExampleMatcher exampleMatcher = ExampleMatcher.matching()
                .withMatcher("empName", ExampleMatcher.GenericPropertyMatchers.exact());
        Example<EmpInfo> example = Example.of(empInfo, exampleMatcher);
        return empRepository.findAll(example, pageable);
    }
}
